﻿using KinonekoSoftware.Foundation.Languages;
using KinonekoSoftware.Foundation.UI.Commands;
using KinonekoSoftware.Foundation;

namespace KinonekoSoftware.Foundation.UI.ViewModels
{

    public sealed class TripleSelectionViewModel : DialogBase
    {
        internal readonly TaskCompletionSource<TripleSelection> TaskCompletionSource;

        private DialogCloseHandler     _handler;
        private DialogExceptionHandler _exHandler;
        private string                 _option1ButtonText;
        private string                 _option2ButtonText;

        public TripleSelectionViewModel()
        {
            TaskCompletionSource = new TaskCompletionSource<TripleSelection>();
            Option1Command       = new ActionCommand(FinishWithOption1);
            Option2Command       = new ActionCommand(FinishWithOption2);
            CancelCommand        = new ActionCommand(Cancel);
        }
        
        internal override void OnReceiveInternal(DialogContext dc)
        {
            _handler   = dc.CloseHandler;
            _exHandler = dc.ExceptionHandler;

            if (dc.Has(DialogContext.Key_Option1ButtonText) &&
                dc.Has(DialogContext.Key_Option2ButtonText) &&
                dc.Has(DialogContext.Key_Description))
            {
                CancelButtonText  = LanguageManager.CancelButtonText;
                Option1ButtonText = dc.GetString(DialogContext.Key_Option1ButtonText);
                Option2ButtonText = dc.GetString(DialogContext.Key_Option2ButtonText);
                Description       = dc.GetString(DialogContext.Key_Description);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override bool CanFinish() => true;


        /// <summary>
        /// 
        /// </summary>
        public void Cancel()
        {
            //
            //
            TaskCompletionSource.SetResult(TripleSelection.Cancel);

            //
            // 关闭对话框
            _handler(this);
        }

        /// <summary>
        /// 尝试完成对话框，并关闭对话框
        /// </summary>
        public void FinishWithOption1()
        {
            try
            {
                if (!CanFinish())
                {
                    return;
                }

                TaskCompletionSource.SetResult(TripleSelection.Option1);

                //
                // 关闭对话框
                _handler(this);
            }
            catch(Exception ex)
            {
                _exHandler(this, ex);
                TaskCompletionSource.SetException(ex);
            }
        }

        /// <summary>
        /// 尝试完成对话框，并关闭对话框
        /// </summary>
        public void FinishWithOption2()
        {
            try
            {
                if (!CanFinish())
                {
                    return;
                }

                //
                //
                TaskCompletionSource.SetResult(TripleSelection.Option2);

                //
                // 关闭对话框
                _handler(this);
            }
            catch(Exception ex)
            {
                _exHandler(this, ex);
                TaskCompletionSource.SetException(ex);
            }
        }

        /// <summary>
        /// 获取或设置 <see cref="Option2ButtonText"/> 属性。
        /// </summary>
        public string Option2ButtonText
        {
            get => _option2ButtonText;
            set => SetValue(ref _option2ButtonText, value);
        }
        /// <summary>
        /// 获取或设置 <see cref="Option1ButtonText"/> 属性。
        /// </summary>
        public string Option1ButtonText
        {
            get => _option1ButtonText;
            set => SetValue(ref _option1ButtonText, value);
        }

        public ActionCommand Option1Command { get; }
        public ActionCommand Option2Command { get; }
    }
}